package com.tywj.buscustomerapp.view.item;

import android.graphics.Color;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tywj.buscustomerapp.R;
import com.tywj.buscustomerapp.model.bean.DaysNew;
import com.tywj.buscustomerapp.utils.TimeRender;
import com.tywj.buscustomerapp.utils.myadapter.item.AdapterItem;

public class DaysItem implements AdapterItem<DaysNew.DataBean> {
    private LinearLayout dateLine;
    private TextView day;
    private TextView price;
    private TextView count;
    private OnCheckListener listener;

    public void setListener(OnCheckListener listener) {
        this.listener = listener;
    }
    public interface OnCheckListener{
        void onChecked(DaysNew.DataBean date, int position, View view);
        void onDeleted(DaysNew.DataBean date);
    }

    @Override
    public int getLayoutResId() {
        return R.layout.item_date;
    }

    @Override
    public void bindViews(@NonNull View root) {
        dateLine = root.findViewById(R.id.dateLin);
        day = root.findViewById(R.id.day);
        price = root.findViewById(R.id.price);
        root.setBackgroundColor(root.getResources().getColor(R.color.white));
        day.setTextColor(root.getResources().getColor(R.color.text));
        price.setTextColor(root.getResources().getColor(R.color.text_price));
        count = root.findViewById(R.id.count);
    }

    @Override
    public void setViews() {

    }

    @Override
    public void handleData(final DaysNew.DataBean daysNew, final int position) {
        day.setText(daysNew.getRiqi().substring(8,10));
        price.setText("￥"+daysNew.getPrice());
        count.setVisibility(View.GONE);
        day.setTextColor(dateLine.getResources().getColor(R.color.text_help));
        if (daysNew.isStatus()){
            daysNew.setCanCheck(true);
            day.setTextColor(dateLine.getResources().getColor(R.color.text));
            if (daysNew.getTicketNum()<=15){
//                count.setTextColor(dateLine.getResources().getColor(R.color.text_help));
                count.setVisibility(View.VISIBLE);
                count.setText("少量");
            }
            if (daysNew.isSellOut()){
//                count.setTextColor(dateLine.getResources().getColor(R.color.text_help));
                day.setTextColor(dateLine.getResources().getColor(R.color.text_help));
                count.setVisibility(View.VISIBLE);
                price.setVisibility(View.GONE);
                count.setText("售罄");
                daysNew.setCanCheck(false);
            }
            if (daysNew.isBuy()){
//                count.setTextColor(dateLine.getResources().getColor(R.color.text_help));
                count.setVisibility(View.VISIBLE);
                count.setText("已买");
            }
        }else{
            daysNew.setCanCheck(false);
            price.setVisibility(View.GONE);
        }

        if (daysNew.isCheck()){
            dateLine.setBackgroundDrawable(dateLine.getResources().getDrawable(R.drawable.bg_ori_shape1));
//            day.setTextColor(dateLine.getResources().getColor(R.color.text));
            price.setTextColor(dateLine.getResources().getColor(R.color.text_price));
        }else{
            dateLine.setBackgroundColor(dateLine.getResources().getColor(R.color.white));
//            day.setTextColor(dateLine.getResources().getColor(R.color.text));
            price.setTextColor(dateLine.getResources().getColor(R.color.text_price));
        }

        dateLine.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (daysNew.isCanCheck()){
                    if (daysNew.isCheck()){
                        if (TimeRender.compareTime3(daysNew.getRiqi())) {
                            v.setBackgroundColor(v.getResources().getColor(R.color.white));
                            day.setTextColor(v.getResources().getColor(R.color.text));
                            price.setTextColor(v.getResources().getColor(R.color.text_price));
                            count.setTextColor(dateLine.getResources().getColor(R.color.textgray));
                            daysNew.setCheck(false);
                            listener.onDeleted(daysNew);
                        }
                    }else{
                        v.setBackgroundDrawable(dateLine.getResources().getDrawable(R.drawable.bg_ori_shape1));
                        day.setTextColor(v.getResources().getColor(R.color.white));
                        price.setTextColor(v.getResources().getColor(R.color.text_price));
                        daysNew.setCheck(true);
                        count.setTextColor(dateLine.getResources().getColor(R.color.black));
                        listener.onChecked(daysNew, position, v);
                    }
                }
            }
        });
    }
}
